<?php
require_once 'database.php';
require_once 'configu.php';	
class photo {
	
	/*===============================GETTER========================================*/
    static function getPhotoes($username){
        $query = "SELECT * FROM foto WHERE username='$username'";
        $photoes = database::query($query);
        return $photoes;
    }
	
	static function getPhotoesTagged($myname){
        //$tagged = database::query("SELECT IDFoto, Username FROM fototag WHERE usernametagged='$myname'");
		$photoes = database::query("SELECT * FROM (SELECT IDFoto, Username FROM fototag WHERE usernametagged='$myname') AS tagged INNER JOIN foto USING (IDFoto, Username)");
        return $photoes;
	}
	
	static function getPhotoesAll($myname){
		$allphoto = database::query("SELECT * FROM foto WHERE username='$myname' UNION 
			SELECT * FROM (SELECT IDFoto, Username FROM fototag WHERE usernametagged='$myname') AS tagged INNER JOIN foto USING (IDFoto, Username)
		");
		return $allphoto;
	}
	
	static function getPhoto($username, $idfoto){
        $query = "SELECT * FROM foto WHERE username='$username' and idfoto='$idfoto'";
        $photo = database::query($query);
        return $photo;
	}

    static function getProfPic($username){

        $query = "SELECT * FROM foto WHERE username='$username' AND idfoto=(SELECT profpic FROM user WHERE username='$username')";
        $photoes = database::query($query);
        return $photoes;
    }
	static function getProfPicPath($username){

        $query = "SELECT * FROM foto WHERE username='$username' AND idfoto=(SELECT profpic FROM user WHERE username='$username')";
        $photoes = database::query($query);
		$photo = mysql_fetch_array($photoes);
		if($photo["FileFoto"]==NULL)
        	return 'images/bg-photo.png';
		else
        	return 'users/'.$username.'/'.$photo["FileFoto"];
    }
	
	
	static function getComment($username, $idfoto){

        $query = "SELECT * FROM fotokomentar WHERE username='$username' and idfoto='$idfoto'";
        $comments = database::query($query);
        return $comments;
	}	
	
	static function getTagged($username, $idfoto){

        $query = "SELECT * FROM fototag WHERE username='$username' and idfoto='$idfoto'";
        $tag = database::query($query);
        return $tag;
	}	
	
	static function getCategory(){

        $query = "SELECT * FROM kategori";
        $cate = database::query($query);
        return $cate;
	}
	
	static function setAsAvatar($username, $idfoto){

        $query = "UPDATE  `progin`.`user` SET  `Profpic` =  '$idfoto' WHERE  `user`.`Username` ='$username'";
        $back = database::query($query);
        return $back;
	}
	
	/*===============================SETTER========================================*/
	static function changeDescription($username, $idfoto, $Desc){

        $query = "UPDATE foto SET deskripsi='$Desc' WHERE username='$username' AND idfoto='$idfoto'";
        $back = database::query($query);
        return $back;
	}
	static function changeHakAkses($username, $idfoto, $hakAkses){

        $query = "UPDATE foto SET HakAksesFoto='$hakAkses' WHERE username='$username' AND idfoto='$idfoto'";
        $back = database::query($query);
        return $back;
	}
	static function changeHakKomen($username, $idfoto, $hakKomen){

        $query = "UPDATE foto SET HakKomentarFoto='$hakKomen' WHERE username='$username' AND idfoto='$idfoto'";
        $back = database::query($query);
        return $back;
	}
	static function changeCategory($username, $idfoto, $cate){

        $query = "UPDATE foto SET IDKategori='$cate' WHERE username='$username' AND idfoto='$idfoto'";
        $back = database::query($query);
        return $back;
	}
	static function changeTagged($username, $idfoto, $stringTag){
		require_once 'database.php';
		$tags = explode(",", $stringTag);
		$Ntags = count($tags);	
		$i=0;
		//database::query("DELETE fototag WHERE username='$username' AND idfoto='$idfoto'");
		while ($i < $Ntags){
			if ($tags[$i][0] == '@'){	
				$utags = substr($tags[$i], 1);
				/*if (*/database::query("INSERT INTO fototag (idfoto, usernametagged, username) VALUES ('$idfoto','$utags','$username')");//)
				//photo::addPhotoTag($utags, $username, $idfoto);
				if ($username!=$utags)
				database::query("INSERT INTO notification (username, type, tagger, identifier) VALUES ('$utags',2,'$username','$idfoto')");
			}
			$i++;
		}
	}
	
	static function isPhotoExist($username, $filefoto){

        $query = "SELECT * FROM foto WHERE username='$username' AND filefoto='$filefoto'";
        $hasil = database::query($query);
		$idf = mysql_fetch_array($hasil);
        return isset($idf["IDFOTO"]);			
	}
	
	
	/*===============================OPERATION========================================*/
	
	static function addComment($username, $idfoto, $commentor, $comment){

		$idkomentar = database::query("SELECT * FROM fotokomentar WHERE username='$username' and idfoto='$idfoto' ORDER BY IDKomentar DESC");
		$idkoment = mysql_fetch_array($idkomentar);
		$idkomentarnow = $idkoment["IDKomentar"] +1;
        $query = "INSERT INTO fotokomentar (idkomentar, username, idfoto, komentator, komentar) VALUES ('$idkomentarnow', '$username','$idfoto','$commentor', '$comment')";
        return database::query($query);
	}
	
	static function delComment($username, $idfoto, $idkomen){
		return database::query("DELETE FROM fotokomentar WHERE username='$username' and idfoto='$idfoto' and idkomentar='$idkomen'");
	}
	
	static function addPhoto($username, $filefoto, $tglAmbil, $desc, $cate, $hAkses, $hKomen){
		$id = database::query("SELECT * FROM foto WHERE username='$username' ORDER BY IDFOTO DESC");
		$idf = mysql_fetch_array($id);
		$idfoto = $idf["IDFOTO"] +1;
		database::query("INSERT INTO Foto (Username,IDFoto, Filefoto, tglAmbil, Deskripsi)
        VALUES ('$username','$idfoto', '$filefoto', '$tglAmbil', '$desc')");
		
		photo::changeCategory($username, $idfoto, $cate);
		photo::changeHakAkses($username, $idfoto, $hAkses);
		photo::changeHakKomen($username, $idfoto, $hKomen);
	}
	/*
	static function addPhotoTag($username, $uploader, $idfoto){
		$idf = mysql_fetch_array(database::query("SELECT * FROM foto WHERE username='$username' ORDER BY IDFOTO DESC"));
		$idnewfoto = $idf["IDFOTO"] +1;
		
		$thefoto = mysql_fetch_array(database::query("SELECT * FROM foto WHERE username='$uploader' AND idfoto='$idfoto'"));
		$filefoto = $thefoto['FileFoto'];
		$tglAmbil = $thefoto['tglAmbil'];
		$Deskripsi = $thefoto['Deskripsi'];
		database::query("INSERT INTO Foto (Username, Uploader, IDFoto, Filefoto, tglAmbil, Deskripsi)
        VALUES ('$username','$uploader','$idnewfoto', '$filefoto', '$tglAmbil', '$Deskripsi')");
		
		photo::changeCategory($username, $idnewfoto, $thefoto['IDKategori']);
		photo::changeHakAkses($username, $idnewfoto, $thefoto['HakAksesFoto']);
		photo::changeHakKomen($username, $idnewfoto, $thefoto['HakKomentarFoto']);
		//Mindahin file jika berhasil ngisi ke database
		$addedfoto = mysql_fetch_array(database::query("SELECT * FROM foto WHERE username='$username' AND idfoto='$idnewfoto'"));
		
		if (isset($addedfoto['IDFOTO'])){
			$src = '../users/'.$uploader.'/'.$thefoto['FileFoto'];
			$pathdest = '../users/'.$username.'/';
			while(file_exists($pathdest.$thefoto['FileFoto'])){	$pathdest += '_';}
			$dest = $pathdest.$thefoto['FileFoto'];
			link($src, $dest);	
		}
	}
	*/
	static function deletePhoto($username, $idfoto){
		require_once 'database.php';
		$result = database::query("SELECT * FROM foto WHERE username='$username' and idfoto='$idfoto'");
		$filefoto = mysql_fetch_array($result);
		unlink('../users/'.$username.'/'.$filefoto["FileFoto"]);
		database::query("DELETE FROM foto WHERE username='$username' and idfoto='$idfoto'");
	}
	
	static function doLike($username, $idfoto){
		require_once 'database.php';
		$result = database::query("SELECT * FROM foto WHERE username='$username' and idfoto='$idfoto'");
		$row = mysql_fetch_array($result);
		$like = $row["Likes"]+1;
		database::query("UPDATE foto SET likes='$like' WHERE username='$username' and idfoto='$idfoto'");
	}
	static function doDislike($username, $idfoto){
		require_once 'database.php';
		$result = database::query("SELECT * FROM foto WHERE username='$username' and idfoto='$idfoto'");
		$row = mysql_fetch_array($result);
		$dislike = $row["Dislikes"]+1;
		database::query("UPDATE foto SET dislikes='$dislike' WHERE username='$username' and idfoto='$idfoto'");
	}
	
	static function doNilai($username, $idfoto, $penilai, $nilai){
		require_once 'database.php';
		
		$result = database::query("INSERT INTO fotolikedislike (PhotoOwner, IdFoto, Penilai, IsLiked) 
			VALUES ('$username','$idfoto','$penilai','$nilai')");
		return $result;
	}
	
	static function countLike($username, $idfoto, $islike){
		require_once 'database.php';
		if ($islike>0){
			$result = database::query("SELECT * FROM fotolikedislike WHERE photoowner='$username' AND idfoto='$idfoto' AND isliked>0");
			return mysql_num_rows($result);
		}
		else if ($islike<0) {
			$result = database::query("SELECT * FROM fotolikedislike WHERE photoowner='$username' AND idfoto='$idfoto' AND isliked<0");
			return mysql_num_rows($result);			
		}
		else{
			$resultlike = database::query("SELECT * FROM fotolikedislike WHERE photoowner='$username' AND idfoto='$idfoto' AND isliked>0");
			$resultdislike = database::query("SELECT * FROM fotolikedislike WHERE photoowner='$username' AND idfoto='$idfoto' AND isliked<0");
			return mysql_num_rows($resultlike)+mysql_num_rows($resultdislike);	
		}
	}
	
	static function canLike($username, $idfoto, $penilai){
		if ($username == $penilai) return false;
		else{
			$result = database::query("SELECT * FROM fotolikedislike WHERE photoowner='$username' AND idfoto='$idfoto' AND penilai='$penilai'");
			$row = mysql_fetch_array($result);
			return  !isset($row["IsLiked"]);
		}
	}
	
	
	static function PopularPhoto(){
		return database::query("SELECT IDFoto, username, COUNT( * ) FROM fotokomentar GROUP BY IDFoto, username ORDER BY Count(*) DESC LIMIT 5");
	}
	
	static function Compare($a, $b)
	{
		$i = strcmp($a[0], $b[0]);
		$j = strcmp($a[1], $b[1]);
		if (($j < 0) || ($j > 0))
			return $j;
		else
			return $i;
	}
	
	static function NilaiPhoto(){
		$hasil = array(0);
		$hasil1 = database::query("SELECT IDFoto, Username FROM foto ORDER BY Username, IDFoto");
		$hasil2 = database::query("SELECT IDFoto, PhotoOwner, COUNT( penilai ) FROM fotolikedislike WHERE fotolikedislike.isliked >0 GROUP BY IDFoto, PhotoOwner ORDER BY PhotoOwner, IDFoto");
		$hasil3 = database::query("SELECT IDFoto, PhotoOwner, COUNT( penilai ) FROM fotolikedislike WHERE fotolikedislike.isliked <0 GROUP BY IDFoto, PhotoOwner ORDER BY PhotoOwner, IDFoto");
		$i = mysql_num_rows($hasil1);
		$j = mysql_num_rows($hasil2);
		$k = mysql_num_rows($hasil3);
		
		$b = true;
		$c = true;
		while (!((($j==0)&&($b)) || (($c)&&($k==0))))
		{
			--$i;
			$baris1 = mysql_fetch_array($hasil1);
			if ($b)
			{
				--$j;
				$baris2 = mysql_fetch_array($hasil2);
			}
			if ($c)
			{
				--$k;
				$baris3 = mysql_fetch_array($hasil3);
			}
			$cmp1 = photo::Compare($baris1, $baris2);
			$cmp2 = photo::Compare($baris1, $baris3);
			$cmp3 = photo::Compare($baris2, $baris3);
			if ($cmp3 < 0)
			{
				$c = false;
				if ($cmp1 < 0)
				{
					$b = false;
					$hasil[1][] = array(0, $baris1[0], $baris1[1]);
				}
				else
				{
					$b = true;
					$hasil[1][] = array($baris2[2], $baris1[0], $baris1[1]);
				}
			}
			else if ($cmp3 > 0)
			{
				$b = false;
				if ($cmp2 < 0)
				{
					$c = false;
					$hasil[1][] = array(0, $baris1[0], $baris1[1]);
				}
				else
				{
					$c = true;
					$hasil[1][] = array(0-$baris3[2], $baris1[0], $baris1[1]);
				}
			}
			else
			{
				if ($cmp2 < 0)
				{
					$b = false;
					$c = false;
					$hasil[1][] = array(0, $baris1[0], $baris1[1]);
				}
				else
				{
					$b = true;
					$c = true;
					$hasil[1][] = array($baris2[2]-$baris3[2], $baris1[0], $baris1[1]);
				}
			}
		}
		if ($j==0 && $b) //yg + uda abis
		{
			while (!(($c)&&($k==0)))
			{
				--$i;
				$baris1 = mysql_fetch_array($hasil1);
				if ($c)
				{
					--$k;
					$baris3 = mysql_fetch_array($hasil3);
				}
				$cmp2 = photo::Compare($baris1[0], $baris3[0]);
				if ($cmp2 < 0)
				{
					$c = false;
					$hasil[1][] = array(0, $baris1[0], $baris1[1]);
				}
				else
				{
					$c = true;
					$hasil[1][] = array(0-$baris2[2], $baris1[0], $baris1[1]);
				}
			}
		}
		else if ($k==0 && $c) //yg - uda abis
		{
			while (!(($j==0)&&($b)))
			{
				--$i;
				$baris1 = mysql_fetch_array($hasil1);
				if ($b)
				{
					--$j;
					$baris2 = mysql_fetch_array($hasil2);
				}
				$cmp1 = photo::Compare($baris1[0], $baris2[0]);
				if ($cmp1 < 0)
				{
					$b = false;
					$hasil[1][] = array(0, $baris1[0], $baris1[1]);
				}
				else
				{
					$b = true;
					$hasil[1][] = array($baris2[2], $baris1[0], $baris1[1]);
				}
			}
		}
		if ((($j==0)&&($b)) && (($c)&&($k==0)))
		{
			while ($i>0)
			{
				--$i;
				$baris1 = mysql_fetch_array($hasil1);
				$hasil[1][] = array(0, $baris1[0], $baris1[1]);
			}
		}
		$hasil[0] = count($hasil[1]);
		return $hasil;
	}
	
	static function NewestPhoto(){
		return database::query("SELECT FileFoto, IDFoto, username, tglUpload FROM foto ORDER BY tglUpload DESC LIMIT 5");
	}

	static function GetPath($id, $user)
	{
		$hasil = database::query("SELECT FileFoto FROM foto WHERE IDFoto = $id AND Username = '$user' LIMIT 1");
		if (mysql_num_rows($hasil)>0)
		{
			$baris = mysql_fetch_row($hasil);
			return $baris[0];
		}
		else
			return "1.jpg";
	}
	
}

?>
